package com.ruoyi.elasticsearch.controller;

import com.ruoyi.elasticsearch.entity.Article;
import com.ruoyi.elasticsearch.service.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 仓库 控制层
 * @author: liyang
 * @date: 2025/7/21 15:36
 **/
@RestController
@RequestMapping("/articles")
@RequiredArgsConstructor
public class ArticleController {

    private final ArticleService articleService;

    @PostMapping("/save")
    public Article save(@RequestBody Article article) {
        return articleService.save(article);
    }

    @GetMapping("/{id}")
    public Article getById(@PathVariable String id) {
        return articleService.findById(id).orElse(null);
    }

    @GetMapping("/getAll")
    public Iterable<Article> getAll() {
        return articleService.findAll();
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        articleService.deleteById(id);
    }

    @GetMapping("/search")
    public List<Article> searchByTitle(@RequestParam String keyword) {
        return articleService.searchByTitle(keyword);
    }
}
